﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace ViewMaker.Core.Silverlight
{
    public class LostFocusBehavior : Behavior<UIElement>
    {
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(LostFocusBehavior), new PropertyMetadata(CommandCallback));

        public ICommand Command
        {
            get { return (ICommand)this.GetValue(CommandProperty); }
            set { this.SetValue(CommandProperty, value); }
        }

        private static void CommandCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.LostFocus += new RoutedEventHandler(AssociatedObject_LostFocus);
        }

        void AssociatedObject_LostFocus(object sender, RoutedEventArgs e)
        {
            if (Command == null) return;
            Command.Execute(null);
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.LostFocus -= new RoutedEventHandler(AssociatedObject_LostFocus);
        }



    }
}
